<!DOCTYPE html>
<html>
<head>
    <title>Hall Effect Diagram</title>
</head>
<body>
<canvas id="hallEffectCanvas" width="600" height="550"></canvas>
<script>
    const canvas = document.getElementById('hallEffectCanvas');
    const ctx = canvas.getContext('2d');

    // Helper function to draw an arrow
    function drawArrow(ctx, fromx, fromy, tox, toy, headlen = 10) {
        const dx = tox - fromy;
        const dy = toy - fromy;
        const angle = Math.atan2(toy - fromy, tox - fromx);
        ctx.beginPath();
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }

    // --- Define coordinates for the cuboid ---
    // Top face vertices
    const P = { x: 150, y: 250 };
    const Q = { x: 220, y: 280 };
    const S = { x: 370, y: 200 };
    const R = { x: 440, y: 230 };

    // Vertical displacement vector (for height)
    const v_height = { x: 0, y: 120 };

    // Bottom face vertices
    const E = { x: P.x + v_height.x, y: P.y + v_height.y }; // (150, 370)
    const F = { x: Q.x + v_height.x, y: Q.y + v_height.y }; // (220, 400)
    const H = { x: S.x + v_height.x, y: S.y + v_height.y }; // (370, 320)
    const G = { x: R.x + v_height.x, y: R.y + v_height.y }; // (440, 350)


    // --- Drawing the cuboid ---
    ctx.lineWidth = 1.5;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.font = '16px Arial';

    // Visible edges (solid lines)
    ctx.beginPath();
    // Top face
    ctx.moveTo(P.x, P.y);
    ctx.lineTo(Q.x, Q.y);
    ctx.lineTo(R.x, R.y);
    ctx.lineTo(S.x, S.y);
    ctx.closePath();
    // Front face
    ctx.moveTo(P.x, P.y);
    ctx.lineTo(E.x, E.y);
    ctx.lineTo(F.x, F.y);
    ctx.lineTo(Q.x, Q.y);
    // Right face
    ctx.moveTo(Q.x, Q.y);
    ctx.lineTo(F.x, F.y);
    ctx.lineTo(G.x, G.y);
    ctx.lineTo(R.x, R.y);
    ctx.stroke();

    // Hidden edges (dashed lines)
    ctx.beginPath();
    ctx.setLineDash([4, 4]);
    // Edge EH
    ctx.moveTo(E.x, E.y);
    ctx.lineTo(H.x, H.y);
    // Edge HG
    ctx.moveTo(H.x, H.y);
    ctx.lineTo(G.x, G.y);
    // Edge SH
    ctx.moveTo(S.x, S.y);
    ctx.lineTo(H.x, H.y);
    ctx.stroke();
    ctx.setLineDash([]); // Reset to solid lines


    // --- Drawing Vertex Labels ---
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText('P', P.x - 15, P.y - 15);
    ctx.fillText('S', S.x, S.y - 15);
    ctx.fillText('R', R.x + 15, R.y - 10);
    ctx.fillText('Q', Q.x + 15, Q.y - 10);
    ctx.fillText('E', E.x - 15, E.y + 10);
    ctx.fillText('F', F.x - 15, F.y + 15);
    ctx.fillText('G', G.x + 15, G.y + 10);
    ctx.fillText('H', H.x, H.y + 15);


    // --- Drawing Arrows and Text ---
    // Magnetic field
    const bFieldLabelX = 350;
    const bFieldLabelY = 100;
    ctx.textAlign = 'left';
    ctx.fillText('magnetic field', bFieldLabelX, bFieldLabelY);
    ctx.fillText('flux density B', bFieldLabelX, bFieldLabelY + 20);
    drawArrow(ctx, 300, 140, 300, 210);
    drawArrow(ctx, 330, 140, 330, 210);
    drawArrow(ctx, 360, 140, 360, 210);

    // Incident electrons
    const electronLabelX = 20;
    const electronLabelY = 430;
    ctx.fillText('direction of', electronLabelX, electronLabelY);
    ctx.fillText('incident electrons', electronLabelX, electronLabelY + 20);
    drawArrow(ctx, 40, 310, 140, 310);
    drawArrow(ctx, 40, 330, 140, 330);
    drawArrow(ctx, 40, 350, 140, 350);

    // --- Figure Caption ---
    ctx.textAlign = 'center';
    ctx.fillText('Fig. 8.1', 300, 500);

</script>
</body>
</html>